#include "chatcommand_joinsimplegame.h"
#include "websocket.h"
#include "datastring.h"
#include "parameters.h"
#include "stringbuilder.h"
#include "message.h"
#include "Debug.h"
#include <inttypes.h>
#include "simplechatgame.h"

// joinsimplegame(messageid,gameid,username,gamedata)
// Joins a simple chat game. Appends gamedata to the game's gamedata.
// Returns a simplegamejoined message or an error.

bool chatcommand_joinsimplegame::processmessage(char first_letter,message *received_message,chatclient *client)
{
	if ((first_letter != 'j') 
		|| (received_message->actual_message.substr(0,15)!="joinsimplegame(")) {
		return false;
	}	
	
	Debug debug(__FILE__,__func__,__LINE__);
	message *new_message;
	datastring method_parameters;
	parameters parameters_parsed;
	bool parameter_success = true;
	int64_t messageid;
	datastring gameid;
	datastring username;
	datastring gamedata;
	datastring currentgamedata;
	simplechatgame *game;
	datastring error_message;
	biglist_item<simplechatgame *> *gamesearch;
	biglist_item<simplechatgameuser *> *usersearch;
	
	debug = __LINE__;
	method_parameters = received_message->actual_message.substr(15,received_message->actual_message.length-16);
	method_parameters.null_terminate(); // Used to comply with snprintf.
	parameters_parsed.long_parameter(method_parameters,parameter_success);
	parameters_parsed.string_parameter(method_parameters,parameter_success);
	parameters_parsed.string_parameter(method_parameters,parameter_success);
	parameters_parsed.string_parameter(method_parameters,parameter_success);
		
	debug = __LINE__;
	if (parameter_success) {
		// Give the parameters nice names.
		debug = __LINE__;
		messageid = parameters_parsed.long_parameters[0];
		gameid = parameters_parsed.string_parameters[1];
		username = parameters_parsed.string_parameters[2];		
		gamedata = parameters_parsed.string_parameters[3];
	
		debug = __LINE__;
		if (username.length == 0) {
			error_message = "A username is required.";
			error(client,error_message,messageid);
			return true;
		}
		// Look to see if this gameid is already used.
		gamesearch = the_websocket->find_simple_game(gameid);		
		debug = __LINE__;
		if (gamesearch == nullptr) {
			error_message = "That gameid was not found.";
			error(client,error_message,messageid);
			return true;
		}
		game = gamesearch->item;
		// Look to see if this user is already there.
		usersearch = game->getuser(username);
		if (usersearch == nullptr) {
			// This is a new user.
			if (game->add_client(client,username)) {
				if (gamedata.length > 0) {
					*(game->gamedata) += gamedata;
				}
			} else {
				error_message = "Out of memory joining game.";
				error(client,error_message,messageid);
				return true;
			}
		} else {
			// This user is already there.
			// They probably lost the connection.
			// Change the client connection.
			// This is insecure from hacking in that someone else can steal the client connection for that user and that game.
			// Fixing this would require some sort of password.
			usersearch->item->client = client;
		}		
		debug = __LINE__;
		currentgamedata.clear();
		if (game->gamedata != nullptr) {
			currentgamedata = *(game->gamedata);
		}
		new_message = simplegamejoined(messageid,gameid,currentgamedata,&(game->users));
		game->send_message_to_clients(new_message,nullptr);
		idisposable::dereference((idisposable**)&new_message);
		debug = __LINE__;
	} else {
		debug = __LINE__;
		parameters_not_correct(client,received_message);
	}
	debug = __LINE__;
	return true;
}
